package Controller;

import Pojo.Pay;
import com.alibaba.fastjson.JSON;
import com.alipay.api.AlipayApiException;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayCodeRecoResult;
import com.alipay.api.internal.util.AlipaySignature;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.response.AlipayTradePagePayResponse;
import com.alipay.api.response.AlipayTradePrecreateResponse;
import com.alipay.demo.trade.config.Configs;
import com.alipay.demo.trade.model.result.AlipayF2FPrecreateResult;

import javax.rmi.CORBA.Util;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.Map;

import Util.AliPayUtil2;

public class ZfbServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        Pay pay=new Pay();
        pay.setOut_trade_no("10003");
        pay.setScene("线上");
        pay.setSubject("丁浩大酒店的房间");
        pay.setStore_id("233");
        pay.setTotal_amount("250");
        pay.setTimeout_express("3m");
        pay.setBody("丁浩大酒店，有梦你就来!");

        //调用自己写的工具类
        try {
            AlipayF2FPrecreateResult af= AliPayUtil2.tradePrecreate(pay);
            //判断状态
            switch (af.getTradeStatus()){
                case FAILED:
                    System.out.println("-------失败");
                    break;
                case SUCCESS:
                    AlipayTradePrecreateResponse response1=af.getResponse();
                    String qrCode=response1.getQrCode();
                    System.out.println(qrCode);
                    System.out.println("成功");
                    response.getWriter().print(qrCode);
                    break;
                case UNKNOWN:
                    System.out.println("------网络异常");
                    break;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
